package org.jili.novoland.game;

import java.util.Vector;

import org.jili.novoland.base.Actor;
import org.jili.novoland.base.Layer;
import org.jili.novoland.base.Sprite;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.Log;

public class Role extends Sprite implements Actor {

    private int direct = BattleMap.DIRECT_EAST;
    final static private int[][] directFrame = { { 0, 0, 0, 0 }, { 0, 1, 2, 3 }, { 4, 5, 6, 7 }, { 8, 9, 10, 11 },
            { 12, 13, 14, 15 }, { 16, 17, 18, 19 } };

    final static private int DEFAULT_SPEED = 4;
    final static private int ROLE_WIDTH = 48;
    final static private int ROLE_HEIGHT = 72;

    final static private int STATUS_WAITING = 0;
    final static private int STATUS_MOVING = 1;

    private int status = STATUS_WAITING;

    private int stepX, stepY = 0;

    private Rect moveImg[][] = new Rect[4][4];

    private int moveListCol[];
    private int moveListRow[];
    private int currPoint;

    private int ap = 0;
    private int maxAp = 12;
    private int reconveAp = 0;

    private int hp = 0;
    private int maxHp = 10;
    private int level = 0;
    private int maxLevel = 10;
    private int ability = 0;

    private int strength = 0;
    private int dexterity = 0;
    private int intelligenc = 0;
    private int luck = 0;

    public Role(int tileWidth, int tileHeight, Bitmap img) {
        super(tileWidth, tileHeight, img, ROLE_WIDTH, ROLE_HEIGHT);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                moveImg[i][j] = new Rect(j * getWidth(), i * getHeight(), getWidth(), getHeight());
            }
        }
    }

    public int getAp() {
        return ap;
    }

    public void setAp(int inPoint) {
        ap = inPoint;
    }

    public void setMoveList(int cols[], int rows[]) {
        int count = 1;
        for (int i = 0; i < cols.length; i++) {
            if (cols[i] >= 0 && rows[i] >= 0)
                count++;
        }
        moveListCol = new int[count];
        moveListRow = new int[count];
        int j = 1;
        for (int i = 0; i < cols.length; i++) {
            if (cols[i] >= 0 && rows[i] >= 0) {
                moveListCol[j] = cols[i];
                moveListRow[j] = rows[i];
                j++;
            }
        }

    }

    @Override
    public void paint(int x, int y, Canvas g) {
        super.paint(x + stepX, y + stepY, g);
        // if (moveListCol != null && moveListRow != null) {
        // for (int i = 0; i < sp; i++) {
        // super.paintFrame(x, y, g, moveListCol[i], moveListRow[i], 0);
        // }
        // }

    }

    public void move() {
        if (moveListCol != null && moveListRow != null) {
            status = STATUS_MOVING;
            stepX = stepY = currPoint = 0;
        }
    }

    public synchronized void tick() {
        switch (status) {
        case STATUS_MOVING:
            if (stepX >= getTileWidth()) {
                stepX = 0;
                col++;
                currPoint++;
            } else if (stepX <= -getTileWidth()) {
                stepX = 0;
                col--;
                currPoint++;
            } else if (stepY >= getTileHeight()) {
                stepY = 0;
                row++;
                currPoint++;
            } else if (stepY <= -getTileHeight()) {
                stepY = 0;
                row--;
                currPoint++;
            } else {

            }

            if (currPoint + 1 >= moveListCol.length) {
                status = STATUS_WAITING;
                stepX = 0;
                stepY = 0;
                currPoint = 0;
                notifyAll();
                return;
            }

            int dcol = moveListCol[currPoint + 1] - col;
            int drow = moveListRow[currPoint + 1] - row;
            int nextDirect = Math.abs((2 - dcol) * dcol + (3 - drow) * drow);
            if (nextDirect != direct) {
                setFrameSequence(directFrame[nextDirect]);
                direct = nextDirect;
            }
            nextFrame();
            stepX += (DEFAULT_SPEED * dcol);
            stepY += (DEFAULT_SPEED * drow);

            break;
        default:

        }

    }
}
